package com.smarth.zhizulian.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.utils.AutoUtils;

/**
 * Created by 18865 on 2018/1/23.
 */

public class KumaDialog extends PopupWindow {
    private Activity mContext;
    private LayoutInflater inflater;
    private RelativeLayout rl_all;
    private ImageView iv_background;
    private int width;
    private int height;

    public KumaDialog(Activity context, int width, int height) {
        this.mContext = context;
        this.width = width;
        this.height = height;
        init();
    }

    private void init() {
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.dialog_kuma, null);
        rl_all = contentView.findViewById(R.id.rl_all);
        iv_background = contentView.findViewById(R.id.iv_background);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rl_all.getLayoutParams();
        lp.width = width;
        lp.height = height;
        rl_all.setLayoutParams(lp);
        AutoUtils.auto(contentView);

        this.setContentView(contentView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }

    public KumaDialog setBackgound(int resouceId) {
        iv_background.setImageResource(resouceId);
        return this;
    }

    public void show() {
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        backgroundAlpha(0.4f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    private void backgroundAlpha(float bgAlpha) {
        Activity activity = (Activity) mContext;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }

    public KumaDialog addWidget(final DialogView dialogView) {
        View view = null;
        if (dialogView.view == null) {
            view = new View(mContext);
        } else {
            view = dialogView.view;
        }
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(dialogView.width, dialogView.height);
        lp.leftMargin = dialogView.marginLeft;
        lp.topMargin = dialogView.marginTop;
        lp.rightMargin = dialogView.marginRight;
        lp.bottomMargin = dialogView.marginBottom;
        if (dialogView.isCenter) {
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        }
        if (dialogView.isBottom) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        }
        if (dialogView.isRight) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }
        if (dialogView.isCenterHorizontal) {
            lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        }
        if (dialogView.isCenterVertical) {
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
        }
        view.setLayoutParams(lp);
        if (dialogView.onClickListener != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KumaDialog.this.dismiss();
                    dialogView.onClickListener.onClick(v);
                }
            });
        }
        AutoUtils.auto(view);
        rl_all.addView(view);
        return this;
    }

    public static class DialogView {
        public static int WRAP_CONTENT = RelativeLayout.LayoutParams.WRAP_CONTENT;

        private View.OnClickListener onClickListener;
        private View view;
        private int marginTop;
        private int marginLeft;
        private int marginRight;
        private int marginBottom;
        private boolean isCenter;
        private boolean isRight;
        private boolean isBottom;
        private boolean isCenterVertical;
        private boolean isCenterHorizontal;
        private int width;
        private int height;

        public DialogView(int width, int height) {
            this.width = width;
            this.height = height;
        }

        public DialogView(View view) {
            this.view = view;
            this.width = WRAP_CONTENT;
            this.height = WRAP_CONTENT;
        }

        public DialogView(View view, int width, int height) {
            this.view = view;
            this.width = width;
            this.height = height;
        }

        public DialogView setMarginTop(int marginTop) {
            this.marginTop = marginTop;
            return this;
        }

        public DialogView setMarginLeft(int marginLeft) {
            this.marginLeft = marginLeft;
            return this;
        }

        public DialogView setOnClickListener(View.OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
            return this;
        }

        public DialogView setMarginRight(int marginRight) {
            this.marginRight = marginRight;
            return this;
        }

        public DialogView setMarginBottom(int marginBottom) {
            this.marginBottom = marginBottom;
            return this;
        }

        public DialogView setCenter(boolean center) {
            isCenter = center;
            return this;
        }

        public DialogView setRight(boolean right) {
            isRight = right;
            return this;
        }

        public DialogView setBottom(boolean bottom) {
            isBottom = bottom;
            return this;
        }

        public DialogView setCenterVertical(boolean centerVertical) {
            isCenterVertical = centerVertical;
            return this;
        }

        public DialogView setCenterHorizontal(boolean centerHorizontal) {
            isCenterHorizontal = centerHorizontal;
            return this;
        }
    }


}
